package com.example.check_oa.model.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.check_oa.model.mapper.*;
import com.example.check_oa.model.service.BusinessTripApService;
import com.example.check_oa.utils.GetAntten;
import com.example.comment_oa.pojos.attendance.*;
import com.example.comment_oa.pojos.MouthAt;
import com.example.comment_oa.pojos_vo.attendanceVo.DateToOneVo;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
* @author cjl
* @description 针对表【business_trip_ap】的数据库操作Service实现
* @createDate 2022-10-22 10:45:03
*/
@Service
public class BusinessTripApServiceImpl extends ServiceImpl<BusinessTripApMapper, BusinessTripAp>
    implements BusinessTripApService{
    private String url = "D:\\study\\gitee\\my_springcloud_project01cangku\\capacity\\src\\views\\capacity\\cjl\\img\\busi";
    @Resource
    BusinessTripApMapper businessTripApMapper;
    @Resource
    HolidayApMapper holidayApMapper;
    @Resource
    OvertimeApMapper overtimeApMapper;
    @Resource
    EarlyMapper earlyMapper;
    @Resource
    LateMapper lateMapper;
    @Resource
    AttendanceMapper attendanceMapper;
    @Resource
    ReplacementCardApMapper replacementCardApMapper;
    @Resource
    MouthAtMapper mouthAtMapper;

    @Override
    public Map all(String day, String userNo,int type) {
        String day1 = day.substring(0, 7);
        //每月出差天数
        Integer i = businessTripApMapper.allMouthBussnessDay(day, userNo);
        //每月请假天数
        Integer i1 = holidayApMapper.allMouthHoliDay(day, userNo);
        //每月加班
        QueryWrapper<OvertimeAp> wrapper2=new QueryWrapper<>();
        wrapper2.eq("OT_STATE",1);
        wrapper2.eq("USER_OT",userNo);
        wrapper2.like("OT_START_TIME",day1);
        wrapper2.select("IFNULL(sum(OT_TIME),0) as overDaynum");
        OvertimeAp overtimeAps = overtimeApMapper.selectOne(wrapper2);
        //每月严重早退天数
        QueryWrapper<Early> wrapper3=new QueryWrapper<>();
        wrapper3.eq("USER_EARLY",userNo);
        wrapper3.eq("IS_EARLY",1);
        wrapper3.like("EARLY_TIME",day1);
        if(type==1){
            wrapper3.lt("early_time",day);
        }
        wrapper3.select("IFNULL(count(*),0) as bigEarlyNum");
        Early earlies2 = earlyMapper.selectOne(wrapper3);
        //每月普通早退天数
        QueryWrapper<Early> wrapper4=new QueryWrapper<>();
        wrapper4.eq("USER_EARLY",userNo);
        wrapper4.eq("IS_EARLY",0);
        wrapper4.like("EARLY_TIME",day1);
        if(type==1){
            wrapper4.lt("early_time",day);
        }
        wrapper4.select("IFNULL(count(*),0) as earlyNum");
        Early earlies = earlyMapper.selectOne(wrapper4);
        //严重迟到天数
        QueryWrapper<Late> wrapper5=new QueryWrapper<>();
        wrapper5.eq("user_late",userNo);
        wrapper5.eq("is_late",1);
        wrapper5.like("late_time",day1);
        if(type==1){
            wrapper5.lt("late_time",day);
        }
        wrapper5.select("IFNULL(count(*),0) as bigLateNum");
        Late lates2 = lateMapper.selectOne(wrapper5);
        //普通迟到天数
        QueryWrapper<Late> wrapper6=new QueryWrapper<>();
        wrapper6.eq("user_late",userNo);
        wrapper6.eq("is_late",0);
        wrapper6.like("late_time",day1);
        if(type==1){
            wrapper5.lt("late_time",day);
        }
        wrapper6.select("IFNULL(count(*),0) as lateNum");
        Late lates = lateMapper.selectOne(wrapper6);
        //获取每个月实际出勤天数
        List<String> list = GetAntten.get(day1);
        List<String> strings = GetAntten.queryMonthDay(day1 + "-01", list);
        //当月漏签数
        QueryWrapper<Attendance> wrapper=new QueryWrapper<>();
        wrapper.eq("user_at",userNo);
        wrapper.like("at_time",day1);
        if(type==1){
            wrapper.lt("at_time",day);
        }
        wrapper.and(Q->Q.isNull("AT_STAR_TIME").or().isNull("AT_END_TIME"));
        wrapper.select( "IFNULL(count(*),0) as starNum");
        Attendance attendance = attendanceMapper.selectOne(wrapper);
        System.out.println("缺卡："+attendance.getStarNum());
        int i2 = attendance.getStarNum();

        //每月旷工
        QueryWrapper<Attendance> wrapper7=new QueryWrapper<>();
        wrapper7.eq("user_at",userNo);
        wrapper7.like("at_time",day1);
        if(type==1){
            wrapper7.lt("at_time",day);
        }
        //本月打卡天数
        List<String> stringList=new ArrayList<>();
        List<Attendance> attendances = attendanceMapper.selectList(wrapper7);
        for (int a = 0; a < attendances.size(); a++) {
            stringList.add(DateUtil.formatDate(attendances.get(a).getAtTime()));
        }
        //调休
        List<String> list1 = GetAntten.get(day.substring(0,7));
        //调休and工作日
        List<String> strings1 = GetAntten.queryMonthDay(day, list1);
        //全部出勤日-交集
        List<String> stringList1 = GetAntten.intersectList2(stringList, strings1);
        int kg=0;
        System.out.println("已打卡次数："+stringList1.size());
        List<String> datetoOne=new ArrayList<>();
        if(type==1){
            List<DateToOneVo> dateToOneVos = attendanceMapper.DateToOne(day1 + "-01", day);
            dateToOneVos.forEach(e->datetoOne.add(e.getDate()));
            System.out.println("本月应打卡："+GetAntten.intersectList2(datetoOne, strings1).size());
            if(GetAntten.intersectList2(datetoOne, strings1).size()-stringList.size()<=i1){
                kg=0;
            }else{
                kg=GetAntten.intersectList2(datetoOne, strings1).size()-stringList.size();
            }
        }else{
            System.out.println("应打卡次数："+(strings1.size()-i1));
            if(i+overtimeAps.getOverDaynum()>=strings1.size()){
                kg=0;
            }else{
                kg=strings1.size()-stringList1.size()-i1;
            }
        }

        Map map=new HashMap();
        map.put("businessTripAps",i);
        map.put("holidayAps",i1);
        map.put("overtimeAps",overtimeAps.getOverDaynum());
        map.put("earlies2",earlies2.getBigEarlyNum());
        map.put("earlies",earlies.getEarlyNum());
        map.put("lates2",lates2.getBigLateNum());
        map.put("lates",lates.getLateNum());
        map.put("attendanceDays",stringList1.size());
        map.put("sjatten",strings1.size());
        map.put("qk",i2);
        map.put("kg",kg);
        return map;
    }

    //add出差申请
    @Override
    public int add(BusinessTripAp businessTripAp, MultipartFile file) {
        businessTripAp.setBtStartTime(businessTripAp.getTime().get(0));
        businessTripAp.setBtEndTime(businessTripAp.getTime().get(1));
        //上月已归档
        QueryWrapper<MouthAt> wrapper8=new QueryWrapper<>();
        Date hoApStartTime = businessTripAp.getBtStartTime();
        wrapper8.like("mouth",DateUtil.formatDate(hoApStartTime).substring(0,7));
        List<MouthAt> mouthAts = mouthAtMapper.selectList(wrapper8);
        if(!mouthAts.isEmpty()){
            return 6;
        }
        //输入日期经过的集合
        List<String> betweenDate = GetAntten.getBetweenDate(DateUtil.formatDate(businessTripAp.getBtStartTime()), DateUtil.formatDate(businessTripAp.getBtEndTime()));
        //查看有无重复
        QueryWrapper<BusinessTripAp> wrapper=new QueryWrapper<>();
        wrapper.eq("user_bt",businessTripAp.getUserBt()).and(i->i.eq("bt_ap_state",1).or().eq("bt_ap_state",0));
        List<BusinessTripAp> businessTripAps = businessTripApMapper.selectList(wrapper);
        List list=new ArrayList();
        businessTripAps.forEach(e->{
            list.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getBtStartTime()),DateUtil.formatDate(e.getBtEndTime())));
        });
        if(GetAntten.intersectList2(list,betweenDate).size()>0){
            return 0;
        }
        //请假
        QueryWrapper<HolidayAp> wrapper1=new QueryWrapper<>();
        wrapper1.eq("user_ho",businessTripAp.getUserBt()).and(i->i.eq("ho_state",1).or().eq("ho_state",0));
        List<HolidayAp> holidayAps = holidayApMapper.selectList(wrapper1);
        List<String> betweenDate1=new ArrayList<>();
        holidayAps.forEach(e->{
            betweenDate1.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getHoApStartTime()), DateUtil.formatDate(e.getHoApEndTime())));
        });
        if(GetAntten.intersectList2(betweenDate1,betweenDate).size()>0){
            //出差中有请假
            return 2;
        }
        //加班
        QueryWrapper<OvertimeAp> ovWrapper=new QueryWrapper<>();
        ovWrapper.eq("user_ot",businessTripAp.getUserBt()).and(i->i.eq("ot_state",1).or().eq("ot_state",0));
        List<OvertimeAp> overtimeAps = overtimeApMapper.selectList(ovWrapper);
        List<String> ovList=new ArrayList<>();
        overtimeAps.forEach(e->{
            ovList.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getOtStartTime()),DateUtil.formatDate(e.getOtEndTime())));
        });
        ovList.forEach(e-> System.out.println(e));
        if(GetAntten.intersectList2(ovList,betweenDate).size()>0){
            //出差有加班
            return 3;
        }
        //判断出差是否有节假日
        List<String> holidayList = Arrays.asList(GetAntten.holiday1);
        if(GetAntten.intersectList2(holidayList,betweenDate).size()>0){
            return 4;
        }

        //今年的工作日
        List<String> yearWorkDay = GetAntten.getYearWorkDay();
        List<String> stringList = GetAntten.intersectList2(yearWorkDay, betweenDate);
        businessTripAp.setBtTime((double) stringList.size());
        if(file!=null){
            //获得文件的名称
            String fileName = file.getOriginalFilename();
            fileName = file.getOriginalFilename();
            //取后缀名
            String ext = FilenameUtils.getExtension(fileName);
            //创建一个新的文件名
            fileName = UUID.randomUUID().toString() +"."+ ext;
            File f =  new File(url,fileName);
            try {
                file.transferTo(f);//进行文件转移
            } catch (IOException e) {
                e.printStackTrace();
            }
            String filePath = "capacity/cjl/img/busi"+fileName;
            businessTripAp.setBtPath(filePath);
        }
        return businessTripApMapper.insert(businessTripAp);
    }

    //同意出差
    @Override
    public int yesBusi(String busiId,String yesUser) {
        UpdateWrapper<BusinessTripAp> wrapper=new UpdateWrapper<>();
        wrapper.eq("bt_water",busiId);
        BusinessTripAp businessTripAp=new BusinessTripAp();
        businessTripAp.setBtApState(1);
        businessTripAp.setBtApYestime(new Date());
        businessTripAp.setBtYesUser(yesUser);
        return businessTripApMapper.update(businessTripAp,wrapper);
    }

    @Override
    public int noBusi(String busiId,String yesUser) {
        UpdateWrapper<BusinessTripAp> wrapper=new UpdateWrapper<>();
        wrapper.eq("bt_water",busiId);
        BusinessTripAp businessTripAp=new BusinessTripAp();
        businessTripAp.setBtApState(2);
        businessTripAp.setBtApYestime(new Date());
        businessTripAp.setBtYesUser(yesUser);
        return businessTripApMapper.update(businessTripAp,wrapper);
    }

}




